package com.itjiange.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
 * cookie获取
 *
 * @author ItJiange
 * @date 2023/10/19
 */
@WebServlet("/cookieGet")
public class CookieGetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            String key = cookie.getName();
            String value = cookie.getValue();
//            System.out.println(key + " : " + URLDecoder.decode(value, StandardCharsets.UTF_8));
            System.out.println(key + " : " + value);
        }
    }
}
